package com.wish.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wish.entity.SysDept;
import com.wish.entity.SysDeptVo;
import com.wish.entity.SysMenu;
import com.wish.service.ISysDeptService;
import com.wish.vo.ResultEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 部门管理 前端控制器
 * </p>
 *
 * @author 五组
 * @since 2021-03-22
 */
@RestController
@RequestMapping("/wish/sys-dept")
public class SysDeptController {

    @Autowired
    private ISysDeptService sysDeptService;

    //列表
    @RequestMapping("/getList")
    public List<SysDept> getList(){

        List<SysDept> list =  sysDeptService.getList();

        return list;
    }
    /*//列表
    @RequestMapping("getList")
    public ResultEntity getList(@RequestBody SysDeptVo sysDeptVo){
        QueryWrapper<SysDept> wrapper = new QueryWrapper<>();
        Page<SysDept> page = new Page<>();

        page.setCurrent(sysDeptVo.getPageNum());
        page.setSize(sysDeptVo.getPageSize());
        Page<SysDept> pageInfo = sysDeptService.getList(wrapper,page,"0");
        return ResultEntity.ok(pageInfo);
    }*/
    /*//删除
    @RequestMapping("del")
    public ResultEntity del(Integer id){
        id = 95;
        boolean b = sysDeptService.removeById(id);
        return ResultEntity.ok(b);
    }*/

    //部门上下级
    @RequestMapping("getDepts")
    public List<SysDept> getDepts(Integer id){
        QueryWrapper<SysDept> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id",id);
        return sysDeptService.list(wrapper);
    }

    //添加
    @RequestMapping("add")
    public boolean add(@RequestBody SysDept sysDept){
        boolean save = sysDeptService.save(sysDept);
        return save;
    }

    //修改
    @RequestMapping("upd")
    public boolean upd(@RequestBody SysDept sysDept){
        boolean b = sysDeptService.updateById(sysDept);
        return b;
    }


    //删除
    @RequestMapping("del")
    public Object del(Integer[] ids){
        QueryWrapper <SysDept> wrapper = new QueryWrapper<>();
        wrapper.in("dept_id",ids);
        sysDeptService.remove(wrapper);
        /* for (Integer id :ids) {*/
        QueryWrapper <SysDept> wrapper2 = new QueryWrapper<>();
        wrapper2.in("parent_id",ids);
        sysDeptService.remove(wrapper2);
        /*}*/
        return true;
    }

    @RequestMapping("/getDeptss")
    public ResultEntity getDepts(){
        QueryWrapper<SysDept> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id",0);
        List<SysDept> list = sysDeptService.list(wrapper);
        if(!list.isEmpty()){
            for (SysDept sysDept:list) {
                List<SysDept> sons = getSons(sysDept.getDeptId());
                sysDept.setDepts(sons);
            }
        }
        return ResultEntity.ok(list);
    }


    public List<SysDept> getSons(Long id) {
        QueryWrapper<SysDept> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id", id);
        List<SysDept> list = sysDeptService.list(wrapper);
        if (list == null) {
            return list;
        } else {
            for (SysDept sysDept : list) {
                List<SysDept> sons = getSons(sysDept.getDeptId());
                sysDept.setDepts(sons);
            }
            return list;
        }
    }


    //回显
    @RequestMapping("getOneId")
    public SysDept getOneId(Integer id){
        System.out.println("========="+id);
        SysDept list =  sysDeptService.getOneId(id);
        System.out.println("*********"+list);
        return list;
    }



}
